
//仅仅接收处理响应

//可能需要到的注解@RestController

package com.rope.backEnd.controller;

import com.rope.backEnd.pojo.Result;
import com.rope.backEnd.pojo.Share;
import com.rope.backEnd.service.ShareService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@Controller
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class ShareController {
    @Autowired
    private ShareService shareService;

    // 指定请求方式1
   //@RequestMapping(value = "/shareSUL",method = RequestMethod.GET)

    //指定请求方式2
    @GetMapping("/shareSUL")
    @ResponseBody
    public Result list(){
        //记录日志
        log.info("查询全部数据");

        //调用service查询数据
        List<Share> shares = shareService.list();

        return Result.success(shares);
    }



    //将上传的“发布信息”传到数据库
    @PostMapping("/publishFrom")
    public ResponseEntity<String> publishFrom(@RequestBody Share publish) {
        if(publish == null){
            return ResponseEntity.ok("上传信息有误");//上传信息有误
        }
        else{
            int outcome = shareService.add(publish);
            if(outcome != 0){
                return ResponseEntity.ok("发布上传成功");//登陆成功
            }
            else {
                return ResponseEntity.ok("发布上传失败");//登陆成功
            }


        }

    }

    @PostMapping("/publishSelectedItem")
    @ResponseBody
    public Result publishList(@RequestBody int publishUserID){
        //记录日志
        log.info("查询用户的所有上传分享数据");

        //调用service查询数据
        List<Share> shares = shareService.publishList(publishUserID);

        return Result.success(shares);
    }


    //修改上传发布数据，即修改publish数据状态
    @PostMapping("/revisePublish")
    @ResponseBody
    public ResponseEntity<String> revisePublish(@RequestBody Share statePublish){
        //记录日志
        log.info("publish数据状态");
        int statePublishFalse =0;
        int statePublishTrue =1;
        int reTu = 0;
        if(statePublish.getPublish() == 1){
            reTu = shareService.state(statePublishFalse,statePublish.getId());
        }
        if(statePublish.getPublish() == 0){
            reTu = shareService.state(statePublishTrue,statePublish.getId());
        }



        if(reTu != 0){
            return ResponseEntity.ok("成功");//修改状态成功
        }
        else {
            return ResponseEntity.ok("失败");//修改状态失败
        }
    }



}
